package pri.hillchen.springboot.shiro.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import pri.hillchen.springboot.shiro.common.service.impl.BaseServiceImpl;
import pri.hillchen.springboot.shiro.common.util.MessageDigestUtil;
import pri.hillchen.springboot.shiro.entity.User;
import pri.hillchen.springboot.shiro.mapper.UserMapper;
import pri.hillchen.springboot.shiro.service.UserService;

import java.util.Set;

/**
 * Created by hillchen on 2017/9/28 0028.
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implements UserService {
    @Override
    public User findUserByUserName(String userName) {
        return baseMapper.findUserByUserName(userName);
    }

    @Override
    public User findUserById(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public User findUserByUserNamePassword(String userName, String passwrod) {
        return baseMapper.findUserByUserNamePassword(userName,passwrod);
    }

    @Override
    public void registUser(User user) {
        user.setPassword(MessageDigestUtil.createCredential(user.getPassword(),user.getUsername()));
        insert(user);
    }

    @Override
    public void assignRoles(String userName, Set<Integer> roleIds) {
        User user = findUserByUserName(userName);
        if(user != null && !CollectionUtils.isEmpty(roleIds)){
            baseMapper.assignRoles(user.getId(),roleIds);
        }
    }
}
